package it.globalgamejam.scar.components 
{
	import flash.display.BitmapData;
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Stamp;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.utils.Input;
	/**
	 * ...
	 * @author N1xx1
	 */
	public class Button extends Entity
	{
		private var click:Function;
		
		public function Button(text:String, x:Number, y:Number, width:Number, height:Number, click:Function=null) 
		{
			super(x, y);
			
			var picture:BitmapData = new BitmapData(width, height, true);
			Draw.setTarget(picture);
			Draw.rect(0, 0, width, height, 0xCCCCCC, 1, true);
			addGraphic(new Stamp(picture));
			
			var _t:Text = new Text(text, 0, 0);
			_t.color = 0x000000;
			_t.size = 32;
			_t.align = "center";
			_t.x = width / 2 - _t.width / 2;
			_t.y = height / 2 - _t.height / 2;
			
			addGraphic(_t);
			setHitbox(width, height);
			
			this.click = click;
		}
		
		public override function update():void
		{
			super.update();
			
			if (click != null) {
				if (Input.mouseReleased) {
					if (collidePoint(x, y, world.mouseX, world.mouseY)) {
						click.call();
					}
				}
			}
		}
		
	}

}